package com.neusoft.neu24.nep.entity;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *
 * ComonResult 控制器返回通用类型，所有的控制器的handler方法的返回值类型都是ComonResult，但是在
 * Service层完成实例化动作
 * 泛型类 类型T的作用在软件运行时指明携带的对象的类型
 * @author Administrator
 * @version 2024/05/27 13:45
 **/
@Data
@NoArgsConstructor
public class CommonResult<T>  {
    private Integer code =200;
    private String msg ="success";
    /**
     *泛型属性 泛型属性在软件运行时指明携带的对象的类型，携带的数据类型
     */
    private T result;

    public CommonResult(ResultCode resultCode, T result) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.result =  result;
    }

    public CommonResult(int  code , String msg ) {
        this.code =code;
        this.msg = msg;

    }

    /**
     * 返回一个200 的状态，并且携带结果
     * @param result
     * @param <T>
     * @return
     */
    public static <T>  CommonResult<T>   success(T result) {
        return new CommonResult<>(ResultCode.SUCCESS, result);
    }


    public static CommonResult  failed(ResultCode code ) {
        CommonResult result =new CommonResult();
        result.setCode( code.getCode()  );
        result.setMsg(  code.getMsg()   );
        result.setResult(null);
        return  result;
    }

    public static CommonResult failed(int code, String msg) {
        return   new CommonResult(code,msg);

    }
}